package com.xiao.xin.core.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xiao.xin.core.Mapper.UserFundMapper;
import com.xiao.xin.core.Mapper.UserFundRecordMapper;
import com.xiao.xin.core.Util.UserThreadLocal;
import com.xiao.xin.core.domain.UserResult;
import com.xiao.xin.core.exception.UserFundException;
import com.xiao.xin.core.domain.UserFund;
import com.xiao.xin.core.domain.UserFundRecord;
import com.xiao.xin.core.service.UserFundRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * user_fund_record Service 接口实现
 * </p>
 *
 * @author Administrator
 * @since 2025-07-18 18:58:17
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class UserFundRecordServiceImpl extends ServiceImpl<UserFundRecordMapper, UserFundRecord> implements UserFundRecordService {

    private final UserFundMapper userFundMapper;


    /***
     * 扣减余额
     * 从账户余额 - 冻结账户
     * @param userId
     * @param money
     */
    @Override
    public void pointsFreeze(Long userId, Integer money) {
        UserFund userFund = getUserFound(userId);

        long score = userFund.getScore() - money;
        if (score >= 0) {
            userFund.setScore(score);
            userFund.setFreezeScore(userFund.getFreezeScore() + money);
            userFundMapper.updateById(userFund);
            saveFundRecord(-money,0,userFund.getId());
            saveFundRecord(money,1,userFund.getId());
        } else {
            throw new UserFundException("账户积分不足");
        }


    }

    /***
     * 归还账户余额
     * 冻结余额 - 账户余额
     * @param userId
     * @param money
     */
    @Override
    public void freezeReturn(Long userId, Integer money) {
        UserFund userFound = getUserFound(userId);

        long temp = userFound.getFreezeScore() - money;

        if (temp >= 0){
            userFound.setFreezeScore(temp);
            userFound.setScore(userFound.getScore() + money);
            userFundMapper.updateById(userFound);
            saveFundRecord(money,0,userFound.getId());
            saveFundRecord(-money,1,userFound.getId());
        }else {
            throw new UserFundException("账户冻结积分不足");
        }
    }


    @Override
    public void pointsDeduction(Long userId, Integer money) {
        UserFund userFound = getUserFound(userId);
        UserFund allFound = getUserFound(0);
        long temp = userFound.getFreezeScore() - money;

        if (temp >= 0){
            userFound.setFreezeScore(temp);
            saveFundRecord(-money,1,userFound.getId());
            userFundMapper.updateById(userFound);

            allFound.setScore(allFound.getScore() + money);
            saveFundRecord(money,0,allFound.getId());
            userFundMapper.updateById(allFound);

        }else {
            throw new UserFundException("账户冻结积分不足");
        }
    }

    @Override
    public void directDeduction(Long userId, Integer money) {

    }



    private void saveFundRecord(Integer money, Integer fundType, Long fundId) {
        save(UserFundRecord
                .builder()
                .money(money)
                .fundType(fundType)
                .fundId(fundId)
                .createdTime(LocalDateTime.now())
                .build());
    }

    private UserFund getUserFound(long userId){
        UserFund userFund = userFundMapper.selectOne(Wrappers.<UserFund>lambdaQuery().eq(UserFund::getUserId, userId));
        if (userFund == null) {
            userFundMapper.insert(UserFund
                    .builder()
                    .userId(userId)
                    .score(0L)
                    .createdTime(LocalDateTime.now())
                    .version(1L)
                    .freezeScore(0L)
                    .build());
        }
        return userFund;
    }
}